clear all
use  "../data/zip_year_data", clear

*Define CHTV and CHTC and the IV 
gen lnchtc = log((chinese100cash - american100cash/american100*chinese100)*(american100/(american100 - american100cash)))
gen chtc = (chinese100cash - american100cash/american100*chinese100)*(american100/(american100 - american100cash))

gen lnchtv = log((chinese100_cashval - american100_cashval/american100_val*chinese100_val)*(american100_val/(american100_val - american100_cashval)))
gen chtv = (chinese100_cashval - american100_cashval/american100_val*chinese100_val)*(american100_val/(american100_val - american100_cashval))

by year, sort: egen chttc = sum(chtc)
gen chi_iv_ct = log(chineseper * chttc)
by year, sort: egen chttv = sum(chtv)
gen chi_iv_val = log(chineseper * chttv)

*Define the interacted terms
gen lnchtvXpost = lnchtv * post
gen lnchtcXpost = lnchtc * post
gen chi_iv_ctXpost = chi_iv_ct * post
gen chi_iv_valXpost = chi_iv_val * post

*Keep only common sample where both lnchtv and lnchtc are not missing
drop if lnchtc == .
drop if lnchtv == .

****Regressions****
*emp t
ivreghdfe lnemp2 lnpopulation d_lnemp2_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 (lnchtv lnchtvXpost = chi_iv_val chi_iv_valXpost) if chtv != 0, absorb(timecounty) cluster(zipcode) 
eststo t1
estadd scalar fstat = e(widstat)
estadd local controls "Yes"
estadd local fe "Yes"

ivreghdfe lnemp2 lnpopulation d_lnemp2_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 (lnchtc lnchtcXpost = chi_iv_ct chi_iv_ctXpost) if chtc != 0, absorb(timecounty) cluster(zipcode) 
eststo t2
estadd scalar fstat = e(widstat)
estadd local controls "Yes"
estadd local fe "Yes"

*emp nt
ivreghdfe lnemp12 lnpopulation d_lnemp12_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 (lnchtv lnchtvXpost = chi_iv_val chi_iv_valXpost) if chtv != 0, absorb(timecounty) cluster(zipcode) 
eststo nt1
estadd scalar fstat = e(widstat)
estadd local controls "Yes"
estadd local fe "Yes"

ivreghdfe lnemp12 lnpopulation d_lnemp12_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 (lnchtc lnchtcXpost = chi_iv_ct chi_iv_ctXpost) if chtc != 0, absorb(timecounty) cluster(zipcode) 
eststo nt2
estadd scalar fstat = e(widstat)
estadd local controls "Yes"
estadd local fe "Yes"

*emp nt - no construction
ivreghdfe lnemp1 lnpopulation d_lnemp12_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 (lnchtv lnchtvXpost = chi_iv_val chi_iv_valXpost) if chtv != 0, absorb(timecounty) cluster(zipcode) 
eststo nt3
estadd scalar fstat = e(widstat)
estadd local controls "Yes"
estadd local fe "Yes"

ivreghdfe lnemp1 lnpopulation d_lnemp12_9600 bachelorshare d_lnincome_9801 ln_density near_college_5 (lnchtc lnchtcXpost = chi_iv_ct chi_iv_ctXpost) if chtc != 0, absorb(timecounty) cluster(zipcode) 
eststo nt4
estadd scalar fstat = e(widstat)
estadd local controls "Yes"
estadd local fe "Yes"

*avg income
ivreghdfe ln_avgincome lnpopulation bachelorshare d_lnincome_9801 ln_density near_college_5 (lnchtv lnchtvXpost = chi_iv_val chi_iv_valXpost) if chtv != 0, absorb(timecounty) cluster(zipcode) 
eststo i1
estadd scalar fstat = e(widstat)
estadd local controls "Yes"
estadd local fe "Yes"

ivreghdfe ln_avgincome lnpopulation bachelorshare d_lnincome_9801 ln_density near_college_5 (lnchtc lnchtcXpost = chi_iv_ct chi_iv_ctXpost) if chtc != 0, absorb(timecounty) cluster(zipcode) 
eststo i2
estadd scalar fstat = e(widstat)
estadd local controls "Yes"
estadd local fe "Yes"

*Create table
esttab nt1 nt2 t1 t2  nt3 nt4 i1 i2 ///
	using "../results/table4.tex", replace ///
	label ///
	keep(lnchtvXpost lnchtv lnchtcXpost lnchtc) ///
	order(lnchtvXpost lnchtv lnchtcXpost lnchtc) ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) ///
	stats(controls fe fstat N , ///
		fmt(0 0 %3.0f %11.0g ) ///
		labels("Controls" ///
			"County-year FE" ///
			"First Stage F-statistic" ///
			"Observations")) ///
	 mlabels(, none) collabels(, none) ///
	 mgroups("Tradable" "Nontradable" "Nontradable - Non Construction" "Average Income", pattern(1 0 1 0 1 0))
	
